package club.ruanx.auth.vcode.factory;

import club.ruanx.auth.vcode.enumeration.ValidateCodeType;
import club.ruanx.util.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 阮胜
 * @date 2018/9/30 13:44
 */
@Component
public class ValidateCodeProcessorManager {
    /**
     * 收集系统中所有的 {@link ValidateCodeGenerator} 接口的实现。
     */
    private final Map<String, ValidateCodeProcessor> validateCodeProcessorMap;

    private static final String VALIDATE_CODE_PROCESSOR_SUFFIX = "CodeProcessor";

    public ValidateCodeProcessorManager(Map<String, ValidateCodeProcessor> validateCodeProcessorMap) {
        this.validateCodeProcessorMap = validateCodeProcessorMap;
    }

    public ValidateCodeProcessor findValidateCodeProcessor(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        return findValidateCodeProcessor(ValidateCodeType.valueOf(type.toUpperCase()));
    }

    public ValidateCodeProcessor findValidateCodeProcessor(ValidateCodeType validateCodeType) {
        String generatorName = validateCodeType.name().toLowerCase() + VALIDATE_CODE_PROCESSOR_SUFFIX;
        return validateCodeProcessorMap.get(generatorName);
    }
}